layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 驾驶员信息表管理
     */
    var BasicDriver = {
        tableId: "basicDriverTable"
    };

    /**
     * 初始化表格的列
     */
    BasicDriver.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'Primary Key'},
            {align: 'center', toolbar: '#tableBar', title: 'Actions',width:150},
            {field: 'driverName', title: 'Name', sort: true},
            {field: 'driverOperateName', title: 'Project', sort: true},
            {field: 'driverNumber', title: 'Jilu Account', sort: true},
            {field: 'six', title: 'Gender'},
            {field: 'tel', title: 'Phone'},
            {field: 'sijileibie', title: 'Category'},
            {field: 'idcard', title: 'ID Card'},
            {field: 'age', title: 'Age'},
            {field: 'drivetype', title: 'Driving Type'},
            {field: 'urgentpeople', title: 'Emergency Contact'},
            {field: 'urgentpeopletel', title: 'Emergency Contact Phone'},
            {field: 'suozaichengshi', title: 'City'},
            {field: 'xiangxidizhi', title: 'Address'},
            
            {field: 'drivenumber', title: 'Driver License Number'},
            {field: 'zhunjiachexing', title: 'Permitted Vehicle Type'},
            
            {field: 'practitionersnumber', title: 'Practice Certificate Number'},
            
            {field: 'notes', title: 'Notes'},

            {field: 'payee', sort: true, title: 'Payee'},
            {field: 'bank', sort: true, title: 'Bank'},
            {field: 'bankAddress', sort: true, title: 'Bank Address'},
            {field: 'bankAccount', sort: true, title: 'Bank Account'},
            {field: 'bankNo', sort: true, title: 'Bank No.'},
            {field: 'cycle', sort: true, title: 'Settlement Cycle'},
            {field: 'taxRate', sort: true, title: 'Tax Rate'},
            {field: 'oilCardNumber', sort: true, title: 'Oil Card Number'},
            {field: 'createTime', title: 'Creation Time'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicDriver.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['driverOperateId'] = $("#driverOperateId").val();
        table.reload(BasicDriver.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };
    
    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: 'Driver Import',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/basicDriver/toimportExcel',
            end: function () {
                table.reload(BasicDriver.tableId);
            }
        });
    });

    /**
     * 弹出添加对话框
     */
    BasicDriver.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/basicDriver/add';
    };

    /**
     * 导出excel按钮
     */
    BasicDriver.exportExcel = function () {
        var checkRows = table.checkStatus(BasicDriver.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BasicDriver.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/basicDriver/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BasicDriver.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/basicDriver/delete", function (data) {
                Feng.success("Deletion successful!");
                table.reload(BasicDriver.tableId);
            }, function (data) {
                Feng.error("Deletion failed! " + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure you want to delete?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicDriver.tableId,
        url: Feng.ctxPath + '/basicDriver/list',
        page: true,
        limit: 40,
		limits:[60,120,240,360,480,600,2000,5000,20000],
        height: "full-60",
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cellMinWidth: 100,
        cols: BasicDriver.initColumn()
    });

    // 批量提交
    $('#btnSubmission').click(function () {
        var checkStatus = table.checkStatus('basicDriverTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/basicDriver/synchronization?ids=" + ids, function (data) {
                    if (data.success) {
                        Feng.success("Synchronization successful!");
                        table.reload(BasicDriver.tableId);
                    } else {
                        Feng.error("Synchronization failed! " + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("Synchronization failed! " + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("Are you sure you want to synchronize selected details?", operation);
        } else {
            layer.alert('Please select at least one record');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicDriver.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BasicDriver.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicDriver.exportExcel();
    });

    $('#weChat').click(function () {
        layer.open({
            type: 2,
            title: 'Driver WeChat Registration Code',
            area: ['1000px', '550px'],
            anim: 5,
            content: Feng.ctxPath + '/basicDriver/toqrCode',
            end: function () {
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + BasicDriver.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BasicDriver.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BasicDriver.onDeleteItem(data);
        }
    });
});
